package com.cqhfan.chatroom.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.cqhfan.chatroom.service.IUserService;
import com.cqhfan.common.autoconfigure.minio.MinioProperties;
import com.cqhfan.common.exceptions.BizException;
import com.cqhfan.common.utils.MinIOUtils;
import com.cqhfan.common.utils.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 眭传洪
 * @create 2024-11-18 17:05
 */
@Tag(name = "文件相关接口")
@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final MinioProperties minioProperties;

    private final IUserService userService;

    @Operation(summary = "上传头像")
    @PutMapping("/uploadAvatar")
    public String uploadAvatar(MultipartFile file) {
        // 校验文件
        if (file == null) {
            throw new BizException("file is null.");
        }

        // 生成并拼接文件名
        String fileName = UUID.fastUUID().toString(true);
        String originalFilename = file.getOriginalFilename();
        if (StrUtil.isBlank(originalFilename)) {
            throw new BizException("file is fail.");
        }
        Long userId = UserContext.getUser();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = "avatar/" + userId + "/" + fileName + fileExtension;

        // 上传文件
        try {
            MinIOUtils.uploadFile(
                    minioProperties.getBucketName(),
                    fileName,
                    file.getInputStream()
            );
        } catch (Exception e) {
            throw new BizException("file import fail.");
        }

        // 拼接文件路径
        String avatarUrl = minioProperties.getFileHost() +
                '/' +
                minioProperties.getBucketName() +
                '/' +
                fileName;
        log.info("文件url: {}", avatarUrl);

        // 更新用户头像
        userService.updateAvatar(userId, avatarUrl);

        return avatarUrl;
    }

    @Operation(summary = "上传头像")
    @PostMapping("/uploadGroupAvatar")
    public String uploadGroupAvatar(MultipartFile file) {
        // 校验文件
        if (file == null) {
            throw new BizException("file is null.");
        }

        // 生成并拼接文件名
        String fileName = UUID.fastUUID().toString(true);
        String originalFilename = file.getOriginalFilename();
        if (StrUtil.isBlank(originalFilename)) {
            throw new BizException("file is fail.");
        }
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = "groupAvatar/" + fileName + fileExtension;

        // 上传文件
        try {
            MinIOUtils.uploadFile(
                    minioProperties.getBucketName(),
                    fileName,
                    file.getInputStream()
            );
        } catch (Exception e) {
            throw new BizException("file import fail.");
        }

        // 拼接文件路径
        String groupAvatarUrl = minioProperties.getFileHost() +
                '/' +
                minioProperties.getBucketName() +
                '/' +
                fileName;
        log.info("文件url: {}", groupAvatarUrl);

        return groupAvatarUrl;
    }

}
